package com.briup.ch01;

import org.junit.Test;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;

/**
 * @author yuxi
 * @version 1.0
 * @date 2025/11/5
 * @Description:
 */
public class DDLTest {
    private static String driverClass="com.mysql.cj.jdbc.Driver";
    private static String url="jdbc:mysql://localhost:3306/briup";
    private static String user="briup";
    private static String password="briup";
    //方法一：新建表
    @Test
    public void createTable(){
        Connection conn=null;
        Statement stmt=null;
        try {
            //1.驱动类加载
            Class.forName(driverClass);
            //2.获取连接对象
            conn= DriverManager.getConnection(url,user,password);
            //3.通过连接对象创建Statement对象
            stmt=conn.createStatement();
            //4.通过Statement对象执行SQL语句,executeUpdate()方法用于执行DDL和DML语句
            stmt.executeUpdate("CREATE TABLE IF NOT EXISTS user(id INT PRIMARY KEY AUTO_INCREMENT,name VARCHAR(50),age INT)");
            stmt.executeUpdate("INSERT INTO user(name,age) VALUES('Tom',25)");
            stmt.executeUpdate("INSERT INTO user(name,age) VALUES('Jerry',30)");
            stmt.executeUpdate("INSERT INTO user(name,age) VALUES('Tom',28)");
            //5.释放资源
        }catch(Exception e){
            e.printStackTrace();
        }finally{
            if(stmt!=null){
                try {
                    //5.释放资源
                    stmt.close();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
            if(conn!=null){
                try {
                    //5.释放资源
                    conn.close();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }







    }
    //方法二：删除表
}
